package org.raymond.icloud.modules.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.raymond.icloud.apis.admin.entity.TSysOrg;
import org.raymond.icloud.apis.admin.entity.enums.StateEnum;
import org.raymond.icloud.modules.admin.mapper.TSysOrgMapper;
import org.raymond.icloud.modules.admin.service.ITSysOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TSysOrgServiceImpl extends ServiceImpl<TSysOrgMapper, TSysOrg> implements ITSysOrgService {
    private final Long TOP_ORG_ID = 0l;

    @Override
    public List<TSysOrg> getsAssociatedOrg(List<Long> userGroupIdList) {
        if(CollectionUtil.isEmpty(userGroupIdList)) {
            return null;
        }
        return null;
    }

    @Override
    public List<TSysOrg> getsParentOrg(Set<Long> orgIdSet) {
        if(CollectionUtil.isEmpty(orgIdSet)
                || (orgIdSet.size()==1&&TOP_ORG_ID.equals(orgIdSet.iterator().next()))){
            return null;
        }
        LambdaQueryWrapper<TSysOrg> listOrgWrapper = new LambdaQueryWrapper<>();
        listOrgWrapper.in(TSysOrg::getId, orgIdSet);
        listOrgWrapper.eq(TSysOrg::getInheritanceRole, true);
        listOrgWrapper.eq(TSysOrg::getState, StateEnum.NORMAL);
        List<TSysOrg> orgList = list(listOrgWrapper);
        Set<Long> parentOrgIdSet = orgList.stream().mapToLong(TSysOrg::getParentId).boxed().collect(Collectors.toSet());
        List<TSysOrg> parentOrgList = getsParentOrg(parentOrgIdSet);
        if(CollectionUtil.isNotEmpty(parentOrgList)){
            orgList.addAll(parentOrgList);
        }
        return orgList;
    }

    @Override
    public List<TSysOrg> getsAllChildOrdByOrgId(Long orgId) {
        return null;
    }
}
